import configdata from './config'
import cache from './cache'
var timerValue = false
let Authorization = uni.getStorageSync('Authorization')
function timerFn() {
	timerValue = true
	let i = 1
	var timer = setInterval(() => {
		i += 1
		if (i >= 5) {
			timerValue = false
			clearInterval(timer)
		}
	}, 1000)
}
module.exports = {
	config: function(name) {
		var info = null;
		if (name) {
			var name2 = name.split("."); //字符分割
			if (name2.length > 1) {
				info = configdata[name2[0]][name2[1]] || null;
			} else {
				info = configdata[name] || null;
			}
			if (info == null) {
				let web_config = cache.get("web_config");
				if (web_config) {
					if (name2.length > 1) {
						info = web_config[name2[0]][name2[1]] || null;
					} else {
						info = web_config[name] || null;
					}
				}
			}
		}
		return info;
	},
	post: function(urlData, [data = '', show = true] = []) {
		if (show) {
			uni.showLoading({
				mask: true
			})
		}

		let url = this.config("APIHOST") + this.config("APIPATH") + urlData;
		//转发到微果
		// #ifdef H5
		url = this.config("APIPATH") + urlData;
		console.log(url)
		// #endif
		return new Promise((succ, error) => {
			uni.request({
				url: url,
				data: data,
				method: "POST",
				header: {
					'Authorization': Authorization,
				},
				success: function(result) {
					if (show) {
						uni.hideLoading()
					}

					if (result.data.status_code == 500) {

					} else if (result.data.code == 1001) {
						uni.hideLoading()
						if (!timerValue) {
							uni.reLaunch({
								url: '/pages/login/login?code=1001'
							})
							timerFn()
						}
					} 
					succ.call(this, result.data)
					if (result.data instanceof Object) {
						result.data._url = url
					} else {
						result.data += url
					}
					console.log(result.data)
				},
				fail: function(e) {
					if (show) {
						uni.hideLoading()
						uni.showToast({
							title: '通信异常，请稍后再试',
							icon: 'none'
						})
					}
					e.url = url
					console.log(e)
					error.call(this, e)
				}
			})
		})
	},
	get: function(urlData, [data = '', show = true] = []) {
		if (show) {
			uni.showLoading({
				mast: true
			})
		}
		let url = this.config("APIHOST") + this.config("APIPATH") + urlData;

		// #ifdef H5
		url = this.config("APIPATH") + urlData;
		// #endif
		return new Promise((succ, error) => {
			uni.request({
				url: url,
				data: data,
				method: "GET",
				header: {
					"Authorization": Authorization
				},
				success: function(result) {
					if (show) {
						uni.hideLoading()
					}


					if (result.data.status_code == 500) {

					} else if (result.data.code == 1001) {
						uni.hideLoading()
						if (!timerValue) {
							uni.reLaunch({
								url: '/pages/login/login?code=1001'
							})
							timerFn()
						}
					}
					succ.call(this, result.data)
					if (result.data instanceof Object) {
						result.data._url = url
					} else {
						result.data += url
					}
					console.log(result.data)
				},
				fail: function(e) {
					if (show) {
						uni.hideLoading()
						uni.showToast({
							title: '通信异常，请稍后再试',
							icon: 'none'
						})
					}
					e.url = url
					console.log(e)
					error.call(this, e)
				}
			})
		})
	}
}
